package dex.micros_gdc

import org.springframework.dao.DataIntegrityViolationException

class TestcaseController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

	// Export service provided by Export plugin
	def exportService
	def grailsApplication  //inject GrailsApplication

    def index() {
        redirect(action: "list", params: params)
    }

    def list(Integer max) {
        params.max = Math.min(max ?: 10, 100)

		def results = Testcase.list(params)

		exportTestcases(params)

        [testcaseInstanceList: results, testcaseInstanceTotal: results.totalCount]
    }

    def create() {
        [testcaseInstance: new Testcase(params)]
    }

    def save() {
        def testcaseInstance = new Testcase(params)
        if (!testcaseInstance.save(flush: true)) {
            render(view: "create", model: [testcaseInstance: testcaseInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'testcase.label', default: 'Testcase'), testcaseInstance.id])
        redirect(action: "show", id: testcaseInstance.id)
    }

    def show(Long id) {
        def testcaseInstance = Testcase.get(id)
        if (!testcaseInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'testcase.label', default: 'Testcase'), id])
            redirect(action: "list")
            return
        }

        [testcaseInstance: testcaseInstance]
    }

    def edit(Long id) {
        def testcaseInstance = Testcase.get(id)
        if (!testcaseInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'testcase.label', default: 'Testcase'), id])
            redirect(action: "list")
            return
        }

        [testcaseInstance: testcaseInstance]
    }

    def update(Long id, Long version) {
        def testcaseInstance = Testcase.get(id)
        if (!testcaseInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'testcase.label', default: 'Testcase'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (testcaseInstance.version > version) {
                testcaseInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'testcase.label', default: 'Testcase')] as Object[],
                          "Another user has updated this Testcase while you were editing")
                render(view: "edit", model: [testcaseInstance: testcaseInstance])
                return
            }
        }

        testcaseInstance.properties = params

        if (!testcaseInstance.save(flush: true)) {
            render(view: "edit", model: [testcaseInstance: testcaseInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'testcase.label', default: 'Testcase'), testcaseInstance.id])
        redirect(action: "show", id: testcaseInstance.id)
    }

    def delete(Long id) {
        def testcaseInstance = Testcase.get(id)
        if (!testcaseInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'testcase.label', default: 'Testcase'), id])
            redirect(action: "list")
            return
        }

        try {
            testcaseInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'testcase.label', default: 'Testcase'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'testcase.label', default: 'Testcase'), id])
            redirect(action: "show", id: id)
        }
    }

	private def exportTestcases(params){
		if(params?.format && params.format != "html"){
			response.contentType = grailsApplication.config.grails.mime.types[params.format]
			response.setHeader("Content-disposition", "attachment; filename=testcases.${params.extension}")

			List fields = [
				"projectName",
				"receivedDate",
				"activityNumber",
				"status",
				"dueDate",
				"testcaseStatus",
				"comments",
			]

			Map labels = [
				"projectName":"Project Name",
				"receivedDate":"Received Date",
				"activityNumber": "Activity #",
				"dueDate": "Due Date",
				"status":"Status",
				"testcaseStatus":"Test Case Status",
				"comments":"Comments"]

			Map parameters = ["column.widths": [
					0.15,
					0.15,
					0.12,
					0.25,
					0.20,
					0.20,
					0.50
				]]
			exportService.export(params.format, response.outputStream, Testcase.list(), fields,labels,[:], parameters)
		}
	}
}
